"""
url 管理器
"""
import os

import redis
import yaml

from tzhhone_reptile.log import Log

yaml_path = os.path.join(os.getcwd(), "config.yaml")
file = open(yaml_path, 'r', encoding='utf-8')
data_redis = yaml.load(file, Loader=yaml.FullLoader)["redis"]
file.close()


class UrlManager(Log):
    def __init__(self):
        pass

    def start(self, host=data_redis["host"], port=data_redis["port"], timeout=0):
        pool = redis.ConnectionPool(host=host, port=port, decode_responses=True)
        r = redis.Redis(connection_pool=pool)
        token = r.brpop("bsc:token", timeout)

        if token is not None:
            token = token[1]
            self.logger.info("已从redis读取到 token=%s" % token)
            return token
        else:
            self.logger.warning("未能从redis读取到需要爬取的token")
            return None


url_manager = UrlManager()
url_manager.start()
